package org.example.edusmart.controller.Student;
import jakarta.servlet.http.HttpServletRequest;
import org.example.edusmart.pojo.WorkAnswerPojo;

import org.example.edusmart.request.TeaOrStu.*;

import org.example.edusmart.service.WorkService;
import org.example.edusmart.utils.AliOSSUtil;
import org.example.edusmart.utils.GetTokenInfo;
import org.example.edusmart.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;


@RestController
@RequestMapping("/StudentWork")
public class StuWorkController {

    @Autowired
    private GetTokenInfo getTokenInfo;

    @Autowired
    private WorkService workService;
    @Autowired
    private AliOSSUtil aliOSSUtil;

    /**
     * 获取作业
     */
    @PostMapping("/get/")
    @PreAuthorize("hasAnyAuthority('student_permission', 'teacher_permission')")
    public Result getWork(@RequestBody CourseIdPojo course) {
        int courseId = course.getCourseId();
        List<HashMap<String, Object>> workList = workService.getWork(courseId);
        return new Result(workList,200,"作业获取成功");
    }

    /**
     * 提交作业
     */
    @PostMapping("/hand/")
    @PreAuthorize("hasAuthority('student_permission')")
    public Result handWork(HttpServletRequest request, HandWorkPojo handWork) throws IOException {
        String strUserId = getTokenInfo.getUserIdByJwt(request);
        int userId = Integer.parseInt(strUserId);
        int workId = handWork.getWorkId();
        Integer status = workService.getWorkStatus(workId, userId);
        String answerContent = handWork.getAnswerContent();
        MultipartFile file = handWork.getFile();
        String fileUrl = aliOSSUtil.upload(file);
        WorkAnswerPojo answer = new WorkAnswerPojo();
        answer.setUserId(userId);
        answer.setWorkId(workId);
        answer.setAnswerContent(answerContent);
        answer.setFileUrl(fileUrl);
        if (status == null) {
            workService.handWork(answer);
            return new Result(200,"作业提交成功");
        } else if (status == 1) {
            workService.rehandWork(answer);
            return new Result(200,"作业重新提交成功");
        } else {
            return new Result(404,"教师已经批改，不可修改");
        }
    }

    /**
     * 查看作业成绩
     */
    @PostMapping("/getGrade/")
    @PreAuthorize("hasAuthority('student_permission')")
    public Result handWork(HttpServletRequest request, @RequestBody WorkIdPojo work){
        String strUserId = getTokenInfo.getUserIdByJwt(request);
        int userId = Integer.parseInt(strUserId);
        int workId = work.getWorkId();
        Integer result = workService.getWorkGrade(workId, userId);
        if (result !=  null) {
            return new Result(result, 200,"作业成绩获取成功");
        } else {
            Integer re = workService.getWorkStatus(workId, userId);
            if (re == null) {
                return new Result(403, "作业还未提交");
            } else {
                return new Result(404, "作业还未批改");
            }
        }
    }

}
